<?php
class Owner extends CActiveRecord {
	/**
	 * The followings are the available columns in table 'owner':
	 * @var integer $id
	 * @var string $first_name
	 * @var string $last_name
	 * @var string $gender
	 * @var string $birth_date
	 * @var string $address1
	 * @var string $address2
	 * @var string $address3
	 * @var integer $city_id
	 * @var string $email
	 * @var string $phone_number
	 * @var string $referent
	 * @var integer $service_id
	 * @var string $adviser_initials
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'owner';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array (
			array ('first_name', 'length', 'max' => 100),
			array ('last_name', 'length', 'max' => 100),
			array ('gender', 'length', 'max' => 5),
			array ('address1', 'length', 'max' => 255),
			array ('address2', 'length', 'max' => 255),
			array ('address3', 'length', 'max' => 255),
			array ('phone_number', 'length', 'max' => 255),
			array ('email', 'length', 'max' => 255),
			array ('referent', 'length', 'max' => 255),
			array ('first_name, birth_date, city_id', 'required')
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array (
			'service' => array (self::BELONGS_TO,	'Service', 'service_id'),
			'city' => array (self::BELONGS_TO, 'City', 'city_id'),
			'adviser' => array (self::BELONGS_TO, 'Adviser', 'adviser_initials'),
			'specific_data' => array(self::HAS_MANY, 'OwnerSpecificData', 'owner_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
			'id' => Yii::t('database', 'Id'),
			'first_name' => Yii::t('database', 'First Name'),
			'last_name' => Yii::t('database', 'Last Name'),
			'gender' => Yii::t('database', 'Gender'),
			'birth_date' => Yii::t('database', 'Birth Date'),
			'address1' => Yii::t('database', 'Address1'),
			'address2' => Yii::t('database', 'Address2'),
			'address3' => Yii::t('database', 'Address3'),
			'city_id' => Yii::t('database', 'City'),
			'email' => Yii::t('database', 'Email'),
			'phone_number' => Yii::t('database', 'Phone Number'),
			'referent' => Yii::t('database', 'Referent'),
			'service_id' => Yii::t('database', 'Service'),
			'adviser_initials' => Yii::t('database', 'Adviser Initials')
		);
	}

	public function getFullName()
	{
		return sprintf (
			"%s%s%s", 
		($this->last_name == null ? "" : $this->last_name),
		($this->first_name != null && $this->last_name != null ? " " : ""),
		($this->first_name == null ? "" : $this->first_name)
		);
	}

	public function getAddress()
	{
		$addr = "";
		if (isset($this->address1) && trim($this->address1) != "")
		{
			$addr .= $this->address1 . "\n";
		}
		if (isset($this->address2) && trim($this->address2) != "")
		{
			$addr .= $this->address2 . "\n";
		}
		if (isset($this->address3) && trim($this->address3) != "")
		{
			$addr .= $this->address3 . "\n";
		}
		$addr .= $this->city->postal_code . " " . $this->city->city_name;

		return $addr;
	}
	
	public function beforeSave() {
		$b = parent::beforeSave();
		
		if ($this->birth_date != null && $this->birth_date instanceof Date) {
			$this->birth_date = $this->birth_date->toMysqlDateString(); 
		}
		
		return $b;
	}
	
	
	public function afterFind() {
		$b = parent::afterFind();
		
		if ($this->birth_date != null) {
			$this->birth_date = new Date(strtotime($this->birth_date));
		}
		
		return $b;
	}
	
	public function afterSave() {
		parent::afterSave();
		
		if ($this->birth_date != null) {
			$this->birth_date = new Date(strtotime($this->birth_date));
		}
	}
}
?>